package com.zyasdk;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.igexin.sdk.PushManager;
import com.zyasdk.controll.WindowUtil;
import com.zyasdk.enums.AdType;
import com.zyasdk.enums.KVType;
import com.zyasdk.interfaces.OnStartUpDimissListener;
import com.zyasdk.model.ARModel;
import com.zyasdk.model.Country;
import com.zyasdk.model.Products;
import com.zyasdk.service.GetServerValue;
import com.zyasdk.util.AppInfoUtil;
import com.zyasdk.util.CountryUtils;
import com.zyasdk.util.NotifiUtil;
import com.zyasdk.util.PushUtil;
import com.zyasdk.util.StaticSetting;
import com.zyasdk.util.ZYALogUtils;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * 
 * @ClassName WindowUtils
 * 
 * 
 */
public class WU {
	private com.zyasdk.controll.WindowUtil wu;
	private Context ct = null;
	
	public WU(Context context) {
		ZYALogUtils.e("WU init");
		ct = context;
		wu = com.zyasdk.controll.WindowUtil.getModel(context);
	}

	/**
	 * 设置包号
	 * @param packId
	 */
	public void setPackId(int packId) {
		wu.setPackId(packId);
	}

	/**
	 * 设置项目绑定码
	 * @param pBindKey
	 */
	public void setPBindKey(String pBindKey) {
		wu.setPBindKey(pBindKey);
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case StaticSetting.RESULT_OK:
				ARModel model = (ARModel) msg.getData().getSerializable("result");
				String resultCode = model.getResultCode();
				String reusltContent = model.getReusltContent();
				if(!TextUtils.isEmpty(resultCode) && resultCode.equals("OK")) {
					if(!TextUtils.isEmpty(reusltContent)) {
						try {
							JSONObject object = new JSONObject(reusltContent);
							if(object.get("sdkHandle").equals("true")){
								sendInitSdkBroadcast();
							}else{
								Log.d("zhuoyian","sdkHandle false");
							}
						}catch (Exception e){
							e.printStackTrace();
						}
					}
				}
				break;
			case StaticSetting.RESULT_ERROR:
				break;
			}
			super.handleMessage(msg);
		}
	};
	/**
	 * 启动SDK服务
	 */
	public void startService() {
		wu.startService();
		PushManager.getInstance().initialize(ct,null);
		sendStartBroadcast();
		
		JSONObject json = null;
	    try {
	    	json = new JSONObject();
	        json.put("accesstype","2");
	        json.put("packetid",KVType.PACKID.getValue(ct));
		} catch (JSONException e) {
		    e.printStackTrace();
		}
		
		if(json != null){
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("PBindKey", KVType.PBINDKEY.getValue(ct)));
			params.add(new BasicNameValuePair("data",Base64.encodeToString(json.toString().getBytes(), Base64.DEFAULT)));
			//new CommonServer(this).connectServer(params, handler, null);
			new GetServerValue(ct).connet(StaticSetting.API_SERVICEPATH + "Api/UnifiedAccess", params, 3, handler, null);
//			new GetServerValue(context).connet(StaticSetting.API_SERVICEPATH + "GetAdverts", params, 1, handler, AdType.AD);
		}
		
		Log.d("zhuoyian","zya sdk start!");
	}

	/**
	 * 设置调试模式
	 * @param isDebug
	 */
	public static void setDebug(boolean isDebug) {
		//ZYALogUtils.isDebug = isDebug;
	}

	/**
	 * 显示单条广告
	 */
	public void showAds() {
		wu.showAd(AdType.AD);
	}

	public void showAds(AdType ad) {
		wu.showAd(ad);
	}
	
	/**
	 * 显示产品推广面板
	 * @param mess
	 */
	public void showRecommend(String mess) {
		wu.showRecommend(mess);
	}

	/**
	 * 是否自动弹出广告
	 * @param autoAd
	 */
	public void setAutoAd(boolean autoAd) {
		//ShowUtil.isShowAd = autoAd; //不自动弹广告，由服务器统一控制
	}

	/**
	 * 设置全屏
	 * @param isScreen
	 */
	public void setScreen(boolean isScreen) {
		WindowUtil.isScreen = isScreen;
	}

	/**
	 * 显示启动图
	 * @param icon
	 * @param defBgImgId
	 * @param defView
	 * @param showSecond
	 * @param appName
	 * @param about
	 * @param onStartUpDimissListener
	 */
	public void showStartUp(int icon, int defBgImgId, View defView,
			int showSecond, String appName, String about,
			OnStartUpDimissListener onStartUpDimissListener) {
		wu.setOnStartUpDimissListener(onStartUpDimissListener);
		wu.showStartUp(icon, defBgImgId, defView, showSecond, appName, about);
	}

	/**
	 * 显示横幅视图
	 * @param view
	 */
	public void showBanners(ViewGroup view) {
		wu.showBanners(view);
	}

	/**
	 * 获取国家
	 * @param context
	 * @return
	 */
	public static String getCountry(Context context) {
		Country country = CountryUtils.getCountry(AppInfoUtil.getIMSI(context));
		if (country != null)
			return country.getCode();
		return null;
	}

	/**
	 * 推送
	 * @param context
	 * @param message
	 * @param onPushDDLBackListener 链接回调
	 */
//	public static void push(Context context, String message,
//			OnPushDDLBackListener onPushDDLBackListener) {
//		PushUtil.getInstance(context).toProcess(message, onPushDDLBackListener);
//	}

	/**
	 * 推送
	 * @param context
	 * @param message
	 */
	public static void push(Context context, String message) {
//		push(context, message, null);
		PushUtil.getInstance(context).onMessage(message);
	}

	/**
	 * 下载产品
	 * @param product
	 */
	public void dlPro(Products product) {
		wu.download(product);
	}
	
	/**
	 * 产品推广通知
	 * @param context
	 * @param product
	 */
	public void toNotifi(Context context,Products product){
		NotifiUtil.getInstance(context).notifiProduct(product);;
	}
	
	private void sendStartBroadcast(){
		Intent intent=new Intent();  
        intent.setAction("com.zyasdk.wu.start");  
        intent.putExtra("a","aaa");  
        ct.sendBroadcast(intent);  
	}
	
	private void sendInitSdkBroadcast(){
		Log.d("zhuoyian","sendInitSdkBroadcast");
		Intent intent=new Intent();  
        intent.setAction("com.zyasdk.wu.initsdk");  
        intent.putExtra("a","aaa");  
        ct.sendBroadcast(intent);  
	}
	
}